// pages/results/index.js
import * as echarts from '../../ec-canvas/echarts.min';
import loginFn from '../../utils/login.js'
import urls from '../../utils/urls.js'
var app = getApp();
var option

function initChart(canvas, width, height) {
	var chart = echarts.init(canvas, null, {
		width: width,
		height: height
	});
	canvas.setChart(chart);
	chart.setOption(option);
	return chart;
}

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		ec: {
			onInit: initChart,
			showchart: false, //显示chart
			studentId: 0, //学生id
			standard: [], //评分标准
			recommendList: [] //推荐课程
		}
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		console.log('options:',options);
		this.showresult(options.studentId, options.questionTypeId) //评分标准
		this.getresult() //获取测试结果
		this.setData({
			studentId: options.studentId
		})
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	},
	getresult: function() { //获取测试结果
		var pages = getCurrentPages();
		var prepage = pages[pages.length - 2]
		var results = prepage.data.results
		var names = []
		var scores = []
		for (var i = 0; i < results.length; i++) {
			names[i] = {
				"name": results[i].dimension,
				"max": 100
			}
			scores[i] = results[i].score
		}
		option = {
			backgroundColor: "#ffffff",
			color: ["#37A2DA"],
			tooltip: {},
			xAxis: {
				show: false
			},
			yAxis: {
				show: false
			},
			radar: {
				// shape: 'circle',
				indicator: names
			},
			series: [{
				name: '测评结果',
				type: 'radar',
				data: [{
					value: scores,
					name: '分数'
				}]
			}]
		};
		this.setData({
			showchart: true
		})
	},
	showresult: function(studentId, questionTypeId) { //测评结果评分标准
		var that = this;
		var loginInfo = loginFn()
		loginInfo.questionTypeId = questionTypeId
		loginInfo.studentId = studentId
		wx.request({
			url: urls.questionresul,
			method: 'POST',
			data: loginInfo,
			success: function(res) {
				console.log(res)
				if (res.data.code == '0') {
					var pages = getCurrentPages();
					var prepage = pages[pages.length - 2]
					var results = prepage.data.results
					console.log(results)
					var dimensionList = res.data.data.dimensionList
					for (var i = 0; i < results.length; i++) {
						for (var j = 0; j < dimensionList.length; j++) {
							if (results[i].questionDimensionId == dimensionList[j].questionDimensionId) {
								if (results[i].score > 0 && results[i].score <= 25) {
									results[i].result = dimensionList[j].score01
								} else if (results[i].score > 25 && results[i].score <= 50) {
									results[i].result = dimensionList[j].score12
								} else if (results[i].score > 50 && results[i].score <= 75) {
									results[i].result = dimensionList[j].score23
								} else if (results[i].score > 75 && results[i].score <= 100) {
									results[i].result = dimensionList[j].score34
								}
							}
						}
					}
					console.log(results)

					that.setData({
						standard: results,
						recommendList: res.data.data.recommendList.list
					})
				}
			}
		})
	},
	restestFn: function() { //重新测评
		wx.navigateBack({
			delta: 1
		})
	}
})
